package com.example.demo.security;

import com.example.demo.model.entity.user.User;
import com.example.demo.model.enums.UserStatus;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

public class UserDetailsImpl implements UserDetails {

    private final User user;

    public UserDetailsImpl(User user) {
        this.user = user;
    }

    /**
     * 获取关联的User实体对象
     */
    public User getUser() {
        return this.user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 您的权限实现逻辑
        return Collections.emptyList();
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true; // 账号是否过期（根据需求实现）
    }

    @Override
    public boolean isAccountNonLocked() {
        return user.getStatus() == UserStatus.ENABLED.getCode(); // 使用枚举比较
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true; // 凭证是否过期（根据需求实现）
    }

    @Override
    public boolean isEnabled() {
        return user.getStatus() == UserStatus.ENABLED.getCode(); // 账号是否启用
    }
}